﻿Option Explicit On
Imports System
Imports System.Data
Imports System.Data.SqlTypes
Imports System.Data.SqlClient

Public Class WorkFlow


    '定义模板常量
    Public Const Table_Task_Template As String = "task_template"
    Public Const Table_Task_Transfer_Template As String = "task_transfer_template"
    Public Const Table_Task_Role_Template As String = "task_role_template"
    Public Const Table_Timing_Task_Template As String = "timing_task_template"

    '定义全局数据库连接对象
    Private conn As SqlConnection

    '定义全局数据库连接适配器
    Private dsCommand_WfTemplate As SqlDataAdapter

    '定义查询命令
    Private GetWfTemplateInfoCommand As SqlCommand

    '定义事务
    Private ts As SqlTransaction

    '定义工作流对象引用
    Private WfProjectTask As WfProjectTask
    Private WfProjectMessages As WfProjectMessages
    Private WfProjectTaskAttendee As WfProjectTaskAttendee
    Private WfProjectTaskTransfer As WfProjectTaskTransfer
    Private WfProjectTimingTask As WfProjectTimingTask
    Private WfProjectTrack As WfProjectTrack
    Private ProjectResponsible As ProjectResponsible

    '定义项目引用
    Private project As Project

    '定义工作日志对象引用
    Private WorkLog As WorkLog

    Private WorkflowType As WorkFlowType

    Private commQuery As CommonQuery

    Private staff As Staff

    '构造函数
    Public Sub New(ByVal DbConnection As SqlConnection, ByRef trans As SqlTransaction)
        MyBase.New()
        conn = DbConnection

        '打开数据库连接
        If conn.State = ConnectionState.Closed Then
            conn.Open()
        End If

        dsCommand_WfTemplate = New SqlDataAdapter()

        '引用外部事务
        ts = trans

        '实例化工作流对象
        WfProjectTask = New WfProjectTask(conn, ts)
        WfProjectMessages = New WfProjectMessages(conn, ts)
        WfProjectTaskAttendee = New WfProjectTaskAttendee(conn, ts)
        WfProjectTaskTransfer = New WfProjectTaskTransfer(conn, ts)
        WfProjectTimingTask = New WfProjectTimingTask(conn, ts)
        WfProjectTrack = New WfProjectTrack(conn, ts)

        '实例化项目对象
        project = New Project(conn, ts)

        '实例化工作日志对象
        WorkLog = New WorkLog(conn, ts)

        WorkflowType = New WorkFlowType(conn, ts)

        commQuery = New CommonQuery(conn, ts)

        staff = New Staff(conn, ts)

    End Sub

    '获取工作流模板信息
    Public Function GetWfProjectTaskTemplateInfo(ByVal templateTableName As String, ByVal condition As String) As DataSet

        Dim tempDs As New DataSet()

        Dim strSql As String = "select * from " & templateTableName & " where " & condition
        GetWfTemplateInfoCommand = New SqlCommand(strSql, conn)
        GetWfTemplateInfoCommand.CommandType = CommandType.Text

        With dsCommand_WfTemplate
            .SelectCommand = GetWfTemplateInfoCommand
            .SelectCommand.Transaction = ts
            .Fill(tempDs, Table_Task_Template)
        End With

        Return tempDs

    End Function

    Public Function CreateProcess(ByVal workflowID As String, ByVal projectID As String, ByVal userID As String) As String
        CreateProcess(workflowID, projectID, userID, "1")
    End Function

    '创建工作流
    Public Function CreateProcess(ByVal workflowID As String, ByVal projectID As String, ByVal userID As String, ByVal phase As String) As String

        '获取系统时间
        Dim sysTime As DateTime = Today

        Dim strSql As String

        '获取项目阶段
        Dim tmpTaskPhase, tmpTaskStatus As String
        Dim dsTempProject As DataSet
        If phase = "1" Then
            strSql = "{project_code =" & "'" & projectID & "'" & "}"
            dsTempProject = project.GetProjectInfo(strSql)
            '异常处理
            If dsTempProject.Tables(0).Rows.Count = 0 Then
                Dim wfErr As New WorkFlowErr()
                wfErr.ThrowNoRecordkErr(dsTempProject.Tables(0))
                Throw wfErr
            End If

            tmpTaskPhase = IIf(IsDBNull(dsTempProject.Tables(0).Rows(0).Item("phase")), "", dsTempProject.Tables(0).Rows(0).Item("phase"))
        Else
            tmpTaskPhase = phase
        End If


        '根据业务品种和项目阶段获取模板ID
        strSql = "{service_type=" & "'" & workflowID & "'" & " and isnull(phase,"")=" & "'" & tmpTaskPhase & "'" & " }"
        Dim dsWorkflowType As DataSet = WorkflowType.GetWorkflowTypeInfo(strSql)

        '异常处理
        If dsWorkflowType.Tables(0).Rows.Count = 0 Then
            Dim wfErr As New WorkFlowErr()
            wfErr.ThrowNoRecordkErr(dsWorkflowType.Tables(0))
            Throw wfErr
        End If

        Dim strWorkflowID As String = dsWorkflowType.Tables(0).Rows(0).Item("workflow_id")
        Dim strWorkflow As String = "workflow_id " & "'" & strWorkflowID & "'"


        '在任务表查找是存在方法参数中指定的工作流对象
        '如果不存在，创建工作流对象，否则异常处理
        strSql = "{project_code=" & "'" & projectID & "'" & " and workflow_id " & "'" & strWorkflowID & "'" & "}"
        Dim dsTemp As DataSet = WfProjectTask.GetWfProjectTaskInfo(strSql)
        If dsTemp.Tables(0).Rows.Count <> 0 Then
            Dim wfErr As New WorkFlowErr()
            wfErr.ThrowExistWorkFlowErr()
            Throw wfErr
        Else

            '1、任务模板
            Dim dsTemplate As DataSet = GetWfProjectTaskTemplateInfo("task_template", strWorkflow)

            Dim newRow As DataRow
            Dim i As Integer
            Dim straTime As DateTime = Now
            Dim beginTaskID As String

            '将业务工作流任务模板任务对象添加到任务表
            For i = 0 To dsTemplate.Tables(0).Rows.Count - 1

                newRow = dsTemp.Tables(0).NewRow()
                With newRow
                    .Item("workflow_id") = strWorkflowID
                    .Item("project_code") = projectID
                    .Item("task_id") = dsTemplate.Tables(0).Rows(i).Item("task_id")
                    .Item("sequence") = dsTemplate.Tables(0).Rows(i).Item("sequence")
                    .Item("task_name") = dsTemplate.Tables(0).Rows(i).Item("task_name")
                    .Item("task_type") = dsTemplate.Tables(0).Rows(i).Item("task_type")
                    .Item("apply_tool") = dsTemplate.Tables(0).Rows(i).Item("apply_tool")
                    .Item("parameters") = dsTemplate.Tables(0).Rows(i).Item("parameters")
                    .Item("duration") = dsTemplate.Tables(0).Rows(i).Item("duration")
                    .Item("merge_relation") = dsTemplate.Tables(0).Rows(i).Item("merge_relation")
                    .Item("flow_tool") = dsTemplate.Tables(0).Rows(i).Item("flow_tool")
                    .Item("create_person") = dsTemplate.Tables(0).Rows(i).Item("create_person")
                    .Item("create_date") = dsTemplate.Tables(0).Rows(i).Item("create_date")
                    .Item("project_phase") = dsTemplate.Tables(0).Rows(i).Item("project_phase")
                    .Item("hasMessage") = dsTemplate.Tables(0).Rows(i).Item("hasMessage")
                End With


                '获取工作流的起始活动任务
                If IIf(IsDBNull(dsTemplate.Tables(0).Rows(i).Item("task_type")), "", dsTemplate.Tables(0).Rows(i).Item("task_type")) = "BEGIN" Then
                    beginTaskID = Trim(dsTemplate.Tables(0).Rows(i).Item("task_id"))
                    newRow.Item("start_time") = Now
                End If
                dsTemp.Tables(0).Rows.Add(newRow)
            Next

            WfProjectTask.UpdateWfProjectTask(dsTemp)

            '2、角色模板
            dsTemplate = GetWfProjectTaskTemplateInfo("task_role_template", strWorkflow)
            dsTemp = WfProjectTaskAttendee.GetWfProjectTaskAttendeeInfo("null")

            '将角色模板添加到角色表中
            For i = 0 To dsTemplate.Tables(0).Rows.Count - 1
                newRow = dsTemp.Tables(0).NewRow()
                With newRow
                    .Item("workflow_id") = strWorkflowID
                    .Item("project_code") = projectID
                    .Item("task_id") = dsTemplate.Tables(0).Rows(i).Item("task_id")
                    .Item("role_id") = dsTemplate.Tables(0).Rows(i).Item("role_id")

                    '将工作流其实活动任务的员工ID属性设置为咨询人员ID，并把它的任务状态置为P
                    If Trim(dsTemplate.Tables(0).Rows(i).Item("task_id")) = beginTaskID Then
                        .Item("attend_person") = userID
                        .Item("task_status") = "P"
                    Else
                        .Item("attend_person") = ""
                    End If
                End With
                dsTemp.Tables(0).Rows.Add(newRow)
            Next

            WfProjectTaskAttendee.UpdateWfProjectTaskAttendee(dsTemp)


            '4、定时任务模板
            dsTemplate = GetWfProjectTaskTemplateInfo("timing_task_template", strWorkflow)
            dsTemp = WfProjectTimingTask.GetWfProjectTimingTaskInfo("null")

            '将任务模板添加到任务模板实例表中
            For i = 0 To dsTemplate.Tables(0).Rows.Count - 1
                newRow = dsTemp.Tables(0).NewRow()
                With newRow
                    .Item("workflow_id") = strWorkflowID
                    .Item("project_code") = projectID    '将所有添加任务的工作流ID置为项目编码
                    .Item("task_id") = dsTemplate.Tables(0).Rows(i).Item("task_id")
                    .Item("role_id") = dsTemplate.Tables(0).Rows(i).Item("role_id")
                    .Item("distance") = dsTemplate.Tables(0).Rows(i).Item("distance")
                    .Item("start_time") = "1900-01-01"
                    .Item("message_id") = dsTemplate.Tables(0).Rows(i).Item("message_id")
                    .Item("type") = dsTemplate.Tables(0).Rows(i).Item("type")
                    .Item("time_limit") = dsTemplate.Tables(0).Rows(i).Item("time_limit")
                    .Item("parameter") = dsTemplate.Tables(0).Rows(i).Item("parameter")
                    .Item("create_person") = dsTemplate.Tables(0).Rows(i).Item("create_person")
                    .Item("create_date") = dsTemplate.Tables(0).Rows(i).Item("create_date")
                End With
                dsTemp.Tables(0).Rows.Add(newRow)
            Next

            WfProjectTimingTask.UpdateWfProjectTimingTask(dsTemp)


            '	如果当前任务的项目阶段非空，将项目阶段置为当前任务的阶段值；
            Dim dsTempTask As DataSet

            strSql = "{project_code=" & "'" & projectID & "'" & " and task_id=" & "'" & beginTaskID & "'" & "}"
            dsTempTask = WfProjectTask.GetWfProjectTaskInfo(strSql)

            '异常处理  
            If dsTempTask.Tables(0).Rows.Count = 0 Then
                Dim wfErr As New WorkFlowErr()
                wfErr.ThrowNoRecordkErr(dsTempTask.Tables(0))
                Throw wfErr
            End If

            tmpTaskPhase = Trim(IIf(IsDBNull(dsTempTask.Tables(0).Rows(0).Item("project_phase")), "", dsTempTask.Tables(0).Rows(0).Item("project_phase")))
            tmpTaskStatus = Trim(IIf(IsDBNull(dsTempTask.Tables(0).Rows(0).Item("project_status")), "", dsTempTask.Tables(0).Rows(0).Item("project_status")))


            strSql = "{project_code=" & "'" & projectID & "'" & "}"
            dsTempProject = project.GetProjectInfo(strSql)

            '异常处理  
            If dsTempProject.Tables(0).Rows.Count = 0 Then
                Dim wfErr As New WorkFlowErr()
                wfErr.ThrowNoRecordkErr(dsTempProject.Tables(0))
                Throw wfErr
            End If

            If tmpTaskPhase <> "" Then
                dsTempProject.Tables(0).Rows(0).Item("phase") = tmpTaskPhase
            End If

            '	如果当前任务的项目状态非空，将项目状态置为当前任务的状态

            If tmpTaskStatus <> "" Then
                dsTempProject.Tables(0).Rows(0).Item("status") = tmpTaskStatus
            End If

            project.UpdateProject(dsTempProject)


            finishedTask(strWorkflowID, projectID, beginTaskID, ".T.", userID)



        End If
    End Function


    '结束任务
    Public Function finishedTask(ByVal workFlowID As String, ByVal projectID As String, ByVal finishedTaskID As String, ByVal finishedFlag As String, ByVal userID As String)
        Return finishedTask(workFlowID, projectID, finishedTaskID, finishedFlag, userID, 0)
    End Function

    Public Function finishedTask(ByVal workFlowID As String, ByVal projectID As String, ByVal finishedTaskID As String, ByVal finishedFlag As String, ByVal userID As String, ByVal flag As Integer)
        Dim startTime As DateTime
        Dim tmpTaskName, tmpTaskType, tmpTaskPhase, tmpTaskStatus, tmpStartMode As String
        Dim i, j, k As Integer
        Dim newRow As DataRow

        '如果该任务已提交过,

        '1、如果当前任务ID不存在，抛出工作任务布存在异常
        Dim strSql As String = "{project_code=" & "'" & projectID & "'" & " and task_id=" & "'" & finishedTaskID & "'" & "}"
        Dim dsTempTask As DataSet = WfProjectTask.GetWfProjectTaskInfo(strSql)
        If dsTempTask.Tables(0).Rows.Count = 0 Then
            Exit Function
        Else

            '获取当前任务的名称,任务类型和开始时间
            tmpTaskName = dsTempTask.Tables(0).Rows(0).Item("task_name")
            tmpTaskType = IIf(IsDBNull(dsTempTask.Tables(0).Rows(0).Item("task_type")), "", dsTempTask.Tables(0).Rows(0).Item("task_type"))
            startTime = IIf(IsDBNull(dsTempTask.Tables(0).Rows(0).Item("start_time")), Now, dsTempTask.Tables(0).Rows(0).Item("start_time"))
            tmpTaskPhase = Trim(IIf(IsDBNull(dsTempTask.Tables(0).Rows(0).Item("project_phase")), "", dsTempTask.Tables(0).Rows(0).Item("project_phase")))
            tmpTaskStatus = Trim(IIf(IsDBNull(dsTempTask.Tables(0).Rows(0).Item("project_status")), "", dsTempTask.Tables(0).Rows(0).Item("project_status")))
            tmpStartMode = Trim(IIf(IsDBNull(dsTempTask.Tables(0).Rows(0).Item("start_mode")), "", dsTempTask.Tables(0).Rows(0).Item("start_mode")))

            '读取项目阶段和项目状态
            Dim tmpProjectPhase, tmpProjectStatus As String
            Dim dsTempProject As DataSet

            strSql = "{project_code=" & "'" & projectID & "'" & "}"
            dsTempProject = project.GetProjectInfo(strSql)

            tmpProjectPhase = Trim(IIf(IsDBNull(dsTempProject.Tables(0).Rows(0).Item("phase")), "", dsTempProject.Tables(0).Rows(0).Item("phase")))
            tmpProjectStatus = Trim(IIf(IsDBNull(dsTempProject.Tables(0).Rows(0).Item("status")), "", dsTempProject.Tables(0).Rows(0).Item("status")))

            '2、如果当前任务ID状态为“W”，抛出不能提交暂停的任务异常
            strSql = "{project_code=" & "'" & projectID & "'" & " and task_id=" & "'" & finishedTaskID & "'" & "}"
            Dim dsTempTaskAttendee As DataSet = WfProjectTaskAttendee.GetWfProjectTaskAttendeeInfo(strSql)

            '异常处理  
            If dsTempTaskAttendee.Tables(0).Rows.Count = 0 Then
                Dim wfErr As New WorkFlowErr
                wfErr.ThrowNoRecordkErr(dsTempTaskAttendee.Tables(0))
                Throw wfErr
            End If

            Dim isWaiting As String = Trim(IIf(IsDBNull(dsTempTaskAttendee.Tables(0).Rows(0).Item("task_status")), "", dsTempTaskAttendee.Tables(0).Rows(0).Item("task_status")))
            If isWaiting = "W" Then
                Dim wfErr As New WorkFlowErr
                wfErr.ThrowWaitingTaskErr()
                Throw wfErr
                Exit Function
            End If


        '3、如果任务类型=“ISLAND”，取系统完成时间作为当前任务的完成时间，返回；
        If tmpTaskType = "ISLAND" Then

            '获取工作日志中该项目该任务并且AUTO类型为0的日志
            Dim Worklog As New WorkLog(conn, ts)
            strSql = "{project_code=" & "'" & projectID & "'" & " and task_id=" & "'" & finishedTaskID & "'" & " and auto=0}"
            Dim dsWorklog As DataSet = Worklog.GetWorkLogInfo(strSql)

            For i = 0 To dsWorklog.Tables(0).Rows.Count - 1

                newRow = dsWorklog.Tables(0).Rows(i)
                With newRow

                    '将任务状态置为F,AUTO置为1,项目阶段和状态置为相应值
                    .Item("task_status") = "F"
                        .Item("project_phase") = tmpProjectPhase

                    '如果任务表中有状态，则把该状态记录到工作日志中，否则把项目的状态记录到工作日志中
                    If tmpTaskStatus = "" Then

                        .Item("project_status") = tmpProjectStatus

                    Else
                        .Item("project_status") = tmpTaskStatus

                    End If

                End With
            Next

            Worklog.UpdateWorkLog(dsWorklog)
            Exit Function
        End If


        '获取系统时间
        Dim sysTime As DateTime = Now

        '2008-8-20 yjf add 增加工作流功能:同一任务如果有多人操作,则只要其中一人提交该任务即完成
        If tmpTaskType = "OPT" Then

            '7、[有效的完成了任务]在任务表将当前任务（ProjectID、TaskID、EmployeeID）状态改为“F”完成
            strSql = "{project_code=" & "'" & projectID & "'" & " and task_id=" & "'" & finishedTaskID & "'}"
            dsTempTaskAttendee = WfProjectTaskAttendee.GetWfProjectTaskAttendeeInfo(strSql)

            '异常处理  
            If dsTempTaskAttendee.Tables(0).Rows.Count = 0 Then
                Dim wfErr As New WorkFlowErr
                wfErr.ThrowNoRecordkErr(dsTempTaskAttendee.Tables(0))
                Throw wfErr
            End If

            Dim tempOptPerson As String
            For i = 0 To dsTempTaskAttendee.Tables(0).Rows.Count - 1
                tempOptPerson = dsTempTaskAttendee.Tables(0).Rows(i).Item("attend_person")
                dsTempTaskAttendee.Tables(0).Rows(i).Item("task_status") = "F"
                '调用AACKMassage（ProjectID、TaskID、EmployeeID）自动确认任务消息；
                AACKMassage(workFlowID, projectID, finishedTaskID, tempOptPerson)
                '将员工完成任务时间置为系统时间
                dsTempTaskAttendee.Tables(0).Rows(0).Item("end_time") = sysTime
            Next


        Else
            '7、[有效的完成了任务]在任务表将当前任务（ProjectID、TaskID、EmployeeID）状态改为“F”完成
            strSql = "{project_code=" & "'" & projectID & "'" & " and task_id=" & "'" & finishedTaskID & "'" & " and attend_person=" & "'" & userID & "'" & "}"
            dsTempTaskAttendee = WfProjectTaskAttendee.GetWfProjectTaskAttendeeInfo(strSql)

            '异常处理  
            If dsTempTaskAttendee.Tables(0).Rows.Count = 0 Then
                Dim wfErr As New WorkFlowErr
                wfErr.ThrowNoRecordkErr(dsTempTaskAttendee.Tables(0))
                Throw wfErr
            End If

            dsTempTaskAttendee.Tables(0).Rows(0).Item("task_status") = "F"

            '8,	调用AACKMassage（ProjectID、TaskID、EmployeeID）自动确认任务消息；
            AACKMassage(workFlowID, projectID, finishedTaskID, userID)

            '将员工完成任务时间置为系统时间
            dsTempTaskAttendee.Tables(0).Rows(0).Item("end_time") = sysTime

        End If

        WfProjectTaskAttendee.UpdateWfProjectTaskAttendee(dsTempTaskAttendee) '更新操作

        '9、添加工作日志(当任务类型不为结束是,添加状态为空的工作日志,待任务流转后填充状态)
        'If isTaskStatusEqualP(projectID, finishedTaskID) Then
        If tmpTaskType <> "END" Then
            AddWorkLog(projectID, finishedTaskID, tmpTaskName, userID, "F", startTime, Now, 1, tmpProjectPhase, "", tmpStartMode)
        End If
        'End If

        '10、如果（ProjectID、TaskID）的所有任务状态均为“F” 将定时任务中的当前任务ID的状态置为“E”；
        '否则，返回；

        strSql = "{project_code=" & "'" & projectID & "'" & " and task_id=" & "'" & finishedTaskID & "'" & "}"
        If flag = 0 Then
            dsTempTaskAttendee = WfProjectTaskAttendee.GetWfProjectTaskAttendeeInfo(strSql)

            '判断是否所有任务状态均为“F”
            For i = 0 To dsTempTaskAttendee.Tables(0).Rows.Count - 1
                If dsTempTaskAttendee.Tables(0).Rows(i).Item("task_status") <> "F" Then
                    Exit Function
                End If
            Next
        End If
        'strSql = "{project_code=" & "'" & projectID & "'" & " and task_id=" & "'" & finishedTaskID & "'" & "}"
        'dsTempTaskAttendee = WfProjectTaskAttendee.GetWfProjectTaskAttendeeInfo(strSql)

        ''判断是否所有任务状态均为“F”
        'For i = 0 To dsTempTaskAttendee.Tables(0).Rows.Count - 1
        '    If dsTempTaskAttendee.Tables(0).Rows(i).Item("task_status") <> "F" Then
        '        Exit Function
        '    End If
        'Next

        '将定时任务中的当前任务ID的状态置为“E”
        Dim dsTempTimingTask As DataSet = WfProjectTimingTask.GetWfProjectTimingTaskInfo(strSql)
        If dsTempTimingTask.Tables(0).Rows.Count <> 0 Then
            For i = 0 To dsTempTimingTask.Tables(0).Rows.Count - 1
                dsTempTimingTask.Tables(0).Rows(i).Item("status") = "E"
            Next
        End If

        WfProjectTimingTask.UpdateWfProjectTimingTask(dsTempTimingTask)

        '[提交手动控制任务]如果当前任务的start_mode=“manual”，将start_mode置空，返回
        If tmpStartMode = "manual" Then
            dsTempTask.Tables(0).Rows(0).Item("start_mode") = ""
            WfProjectTask.UpdateWfProjectTask(dsTempTask)
            Exit Function
        End If

        '2005-04-27 yjf 修改：手动任务提交不改变项目状态
        '将项目的阶段置为任务的相应阶段
        strSql = "{project_code=" & "'" & projectID & "'" & "}"
        dsTempProject = project.GetProjectInfo(strSql)

        '异常处理  
        If dsTempProject.Tables(0).Rows.Count = 0 Then
            Dim wfErr As New WorkFlowErr
            wfErr.ThrowNoRecordkErr(dsTempProject.Tables(0))
            Throw wfErr
        End If

        If tmpTaskPhase <> "" Then
            dsTempProject.Tables(0).Rows(0).Item("phase") = tmpTaskPhase
        End If

        project.UpdateProject(dsTempProject)

        '11、如果当前任务提供流程工具，调用流程工具
        Dim tmpFlowTools As String
        Dim args As Object() = {conn, ts}
        If Trim(IIf(IsDBNull(dsTempTask.Tables(0).Rows(0).Item("flow_tool")), "", dsTempTask.Tables(0).Rows(0).Item("flow_tool"))) <> "" Then
            tmpFlowTools = dsTempTask.Tables(0).Rows(0).Item("flow_tool")
            tmpFlowTools = "BusinessRules." & tmpFlowTools
            tmpFlowTools = tmpFlowTools.Trim

            '动态创建接口对象
            Dim t As System.Type = System.Type.GetType(tmpFlowTools)
            Dim iFlowTools As IFlowTools = Activator.CreateInstance(t, args)
            iFlowTools.UseFlowTools(workFlowID, projectID, finishedTaskID, finishedFlag, userID)

        End If

        '4、获取当前任务的转移任务和转移条件记录集
        strSql = "{project_code=" & "'" & projectID & "'" & " and task_id=" & "'" & finishedTaskID & "'" & "}"
        Dim dsTempTaskTransfer As DataSet = WfProjectTaskTransfer.GetWfProjectTaskTransferInfo(strSql)

        '5、 获取转移条件为真的转移任务集
        Dim nextTaskID, tmpTransCondition, tmpTransPhase, tmpTransStatus As String
        Dim dsConditionTrue As DataSet = WfProjectTaskTransfer.GetWfProjectTaskTransferInfo("null")
        For i = 0 To dsTempTaskTransfer.Tables(0).Rows.Count - 1
            nextTaskID = Trim(dsTempTaskTransfer.Tables(0).Rows(i).Item("next_task"))
            tmpTransCondition = Trim(dsTempTaskTransfer.Tables(0).Rows(i).Item("transfer_condition"))
            tmpTransStatus = Trim(IIf(IsDBNull(dsTempTaskTransfer.Tables(0).Rows(i).Item("project_status")), "", dsTempTaskTransfer.Tables(0).Rows(i).Item("project_status")))

            '判断条件是否为真
            If CompareExpression(workFlowID, projectID, finishedTaskID, finishedFlag, tmpTransCondition) Then

                '构造转移条件为真的转移任务集
                newRow = dsConditionTrue.Tables(0).NewRow()
                With newRow
                    .Item("workflow_id") = ""
                    .Item("project_code") = projectID
                    .Item("project_status") = tmpTransStatus
                    .Item("task_id") = finishedTaskID
                    .Item("next_task") = nextTaskID
                    .Item("transfer_condition") = tmpTransCondition
                    .Item("project_status") = tmpTransStatus
                End With
                dsConditionTrue.Tables(0).Rows.Add(newRow)
            End If
        Next

        '6、如果转移条件为真的转移任务集为空
        If dsConditionTrue.Tables(0).Rows.Count = 0 Then

            '如果当前活动类型不是结束任务，抛出提交任务的结果无效错误,返回。
            If tmpTaskType <> "END" Then
                Dim wfErr As New WorkFlowErr
                wfErr.ThrowInvalidSubmit()
                Throw wfErr
                Exit Function
            End If
        End If



        '12、如果当前任务的类型为“结束”，返回；
        If tmpTaskType = "END" Then

            '如果任务表中有状态，则把该状态记录到工作日志中，否则把项目的状态记录到工作日志中
            ' If isTaskStatusEqualP(projectID, finishedTaskID) Then
            If tmpTaskStatus = "" Then
                '添加工作日志
                AddWorkLog(projectID, finishedTaskID, tmpTaskName, userID, "F", startTime, Now, 1, tmpProjectPhase, tmpProjectStatus, tmpStartMode)
            Else
                AddWorkLog(projectID, finishedTaskID, tmpTaskName, userID, "F", startTime, Now, 1, tmpProjectPhase, tmpTaskStatus, tmpStartMode)
            End If
            'End If

            Exit Function

        End If

        '13、[真正完成了一个有后继活动的任务]对每个转移条件为真的转移任务（多个任务）

        '   如果汇集类型为“AND”，获取转移任务的前趋活动状态；

        Dim tmpNextTaskID As String
        Dim tmpPreTaskID As String
        Dim dsTempPreTaskStatus, dsTempWorkLog As DataSet
        Dim tmpApplyTool As String

        '用FiliterJeeDummyTask（ProjectID、ShiftTaskSet）获取转移条件为真的业务任务（实任务）
        dsConditionTrue = FiliterJeeDummyTask(workFlowID, projectID, dsConditionTrue, finishedFlag, userID)

        '如果实任务集记录为空,证明为项目结束,将工作日志中该任务状态改为项目状态
        If dsConditionTrue.Tables(0).Rows.Count = 0 Then

            strSql = "{project_code=" & "'" & projectID & "'" & " and task_id=" & "'" & finishedTaskID & "'" & " and project_status=''}"
            dsTempWorkLog = WorkLog.GetWorkLogInfo(strSql)

            '重新获取项目状态
            strSql = "{project_code=" & "'" & projectID & "'" & "}"
            dsTempProject = project.GetProjectInfo(strSql)
            tmpProjectStatus = Trim(IIf(IsDBNull(dsTempProject.Tables(0).Rows(0).Item("status")), "", dsTempProject.Tables(0).Rows(0).Item("status")))

            For j = 0 To dsTempWorkLog.Tables(0).Rows.Count - 1
                dsTempWorkLog.Tables(0).Rows(j).Item("project_status") = tmpProjectStatus
            Next
            WorkLog.UpdateWorkLog(dsTempWorkLog)

        End If

        '遍历所有转移条件为真的转移任务集
        Dim tmpNextTaskType, mergeRelation As String
        Dim dsPreTaskMode As DataSet
        Dim tmpPreTaskMode As String

        '2005-09-13 yjf add 修改在有多个后续任务时,由于第一个后续任务前置任务未完成而导致第二个后续任务不处理的情况
        Dim isFinishedPreTask As Boolean

        For i = 0 To dsConditionTrue.Tables(0).Rows.Count - 1

            isFinishedPreTask = True

            tmpNextTaskID = dsConditionTrue.Tables(0).Rows(i).Item("next_task")
            strSql = "{project_code=" & "'" & projectID & "'" & " and task_id=" & "'" & tmpNextTaskID & "'" & "}"
            dsTempTask = WfProjectTask.GetWfProjectTaskInfo(strSql)

            '异常处理  
            If dsTempTask.Tables(0).Rows.Count = 0 Then
                Dim wfErr As New WorkFlowErr
                wfErr.ThrowNoRecordkErr(dsTempTask.Tables(0))
                Throw wfErr
            End If


            tmpNextTaskType = IIf(IsDBNull(dsTempTask.Tables(0).Rows(0).Item("task_type")), "", dsTempTask.Tables(0).Rows(0).Item("task_type"))
            mergeRelation = IIf(IsDBNull(dsTempTask.Tables(0).Rows(0).Item("merge_relation")), "", dsTempTask.Tables(0).Rows(0).Item("merge_relation"))

            tmpTransStatus = Trim(IIf(IsDBNull(dsConditionTrue.Tables(0).Rows(i).Item("project_status")), "", dsConditionTrue.Tables(0).Rows(i).Item("project_status")))

            '调用AddTaskTrackRecord（ProjectID,Workflow_id,TaskID,StartupTask）记录流程历程信息;
            AddTaskTrackRecord("", projectID, finishedTaskID, tmpNextTaskID)

            '如果转移任务的项目状态属性值非空
            '   将项目的状态修改为转移任务的项目状态属性值；
            strSql = "{project_code=" & "'" & projectID & "'" & "}"
            dsTempProject = project.GetProjectInfo(strSql)

            '异常处理  
            If dsTempProject.Tables(0).Rows.Count = 0 Then
                Dim wfErr As New WorkFlowErr
                wfErr.ThrowNoRecordkErr(dsTempProject.Tables(0))
                Throw wfErr
            End If

            If tmpTransStatus <> "" Then
                dsTempProject.Tables(0).Rows(0).Item("status") = tmpTransStatus
                project.UpdateProject(dsTempProject)
            End If

            '获取项目的状态；
            '在工作日志将TaskID任务的项目状态为空的记录的项目状态置为项目的状态值;
            dsTempProject = project.GetProjectInfo(strSql)

            '异常处理  
            If dsTempProject.Tables(0).Rows.Count = 0 Then
                Dim wfErr As New WorkFlowErr
                wfErr.ThrowNoRecordkErr(dsTempProject.Tables(0))
                Throw wfErr
            End If

            tmpProjectStatus = Trim(IIf(IsDBNull(dsTempProject.Tables(0).Rows(0).Item("status")), "", dsTempProject.Tables(0).Rows(0).Item("status")))
            strSql = "{project_code=" & "'" & projectID & "'" & " and task_id=" & "'" & finishedTaskID & "'" & " and project_status=''}"
            dsTempWorkLog = WorkLog.GetWorkLogInfo(strSql)

            For j = 0 To dsTempWorkLog.Tables(0).Rows.Count - 1
                dsTempWorkLog.Tables(0).Rows(j).Item("project_status") = tmpProjectStatus
            Next
            WorkLog.UpdateWorkLog(dsTempWorkLog)

            '获取每一个转移条件为真的转移任务ID

            If mergeRelation = "AND" Then
                '如果汇集类型为“AND”，获取转移任务的前趋活动状态；

                strSql = "{project_code=" & "'" & projectID & "'" & " and next_task=" & "'" & tmpNextTaskID & "'" & " and transfer_condition = '.T.'}"
                '获取转移任务的前趋活动集
                dsTempTaskTransfer = WfProjectTaskTransfer.GetWfProjectTaskTransferInfo(strSql)

                '遍历转移任务的前趋活动集
                For j = 0 To dsTempTaskTransfer.Tables(0).Rows.Count - 1

                    tmpPreTaskID = dsTempTaskTransfer.Tables(0).Rows(j).Item("task_id")
                    strSql = "{project_code=" & "'" & projectID & "'" & " and task_id=" & "'" & tmpPreTaskID & "'" & "}"

                    '获取每个前趋活动任务角色及其完成状态集
                    dsTempPreTaskStatus = WfProjectTaskAttendee.GetWfProjectTaskAttendeeInfo(strSql)

                    '遍历前趋活动的完成状态（是否有为P并且启动模式不为Manual）
                    For k = 0 To dsTempPreTaskStatus.Tables(0).Rows.Count - 1

                        '获取前趋任务的启动模式
                        strSql = "{project_code=" & "'" & projectID & "'" & " and task_id=" & "'" & tmpPreTaskID & "'" & "}"
                        dsPreTaskMode = WfProjectTask.GetWfProjectTaskInfo(strSql)

                        '异常处理  
                        If dsPreTaskMode.Tables(0).Rows.Count = 0 Then
                            Dim wfErr As New WorkFlowErr
                            wfErr.ThrowNoRecordkErr(dsTempProject.Tables(0))
                            Throw wfErr
                        End If

                        tmpPreTaskMode = IIf(IsDBNull(dsPreTaskMode.Tables(0).Rows(0).Item("start_mode")), "", dsPreTaskMode.Tables(0).Rows(0).Item("start_mode"))

                        If IIf(IsDBNull(dsTempPreTaskStatus.Tables(0).Rows(k).Item("task_status")), "", dsTempPreTaskStatus.Tables(0).Rows(k).Item("task_status")) <> "F" And tmpPreTaskMode <> "manual" Then
                            '2005-09-13 yjf add 修改在有多个后续任务时,由于第一个后续任务前置任务未完成而导致第二个后续任务不处理的情况
                            isFinishedPreTask = False
                            'Exit Function
                        End If
                    Next
                Next

                '2005-09-13 yjf add 修改在有多个后续任务时,由于第一个后续任务前置任务未完成而导致第二个后续任务不处理的情况
                If isFinishedPreTask Then

                    '启动转移任务
                    StartupTask(workFlowID, projectID, tmpNextTaskID, "", "", finishedTaskID, userID)

                End If

            Else
                If mergeRelation = "XOR" Then
                    '启动转移任务后返回
                    StartupTask(workFlowID, projectID, tmpNextTaskID, "", "", finishedTaskID, userID)
                    Exit Function
                Else
                    '启动转移任务
                    StartupTask(workFlowID, projectID, tmpNextTaskID, "", "", finishedTaskID, userID)
                End If
            End If
        Next

        End If

    End Function


    '自动确认消息AACKMassage（ProjectID、TaskID、EmployeeID）
    Public Function AACKMassage(ByVal workflowID As String, ByVal projectID As String, ByVal taskID As String, ByVal employeeID As String)

        '①	获取ProjectID的企业名称；
        '获取项目的企业名称
        Dim strSql As String
        strSql = "{project_code=" & "'" & projectID & "'" & "}"
        Dim dsProject As DataSet = project.GetProjectInfo(strSql)

        '异常处理  
        If dsProject.Tables(0).Rows.Count = 0 Then
            Dim wfErr As New WorkFlowErr
            wfErr.ThrowNoRecordkErr(dsProject.Tables(0))
            Throw wfErr
        End If

        Dim tmpCorporationCode As String = dsProject.Tables(0).Rows(0).Item("corporation_code")
        strSql = "{corporation_code=" & "'" & tmpCorporationCode & "'" & "}"
        Dim corporationAccess As New corporationAccess(conn, ts)
        Dim dsCorporation As DataSet = corporationAccess.GetcorporationInfo(strSql, "null")

        '异常处理  
        If dsCorporation.Tables(0).Rows.Count = 0 Then
            Dim wfErr As New WorkFlowErr
            wfErr.ThrowNoRecordkErr(dsCorporation.Tables(0))
            Throw wfErr
        End If

        Dim tmpCorporationName As String = Trim(dsCorporation.Tables(0).Rows(0).Item("corporation_name"))

        '②	获取ProjectID、TaskID的任务名称；
        '②	获取任务ID的任务名称；
        strSql = "{project_code=" & "'" & projectID & "'" & " and task_id=" & "'" & taskID & "'" & "}"
        Dim dsTempTask As DataSet = WfProjectTask.GetWfProjectTaskInfo(strSql)

        '未找到任务则返回  
        If dsTempTask.Tables(0).Rows.Count = 0 Then
            Exit Function
        Else

            Dim tmpTaskName As String = dsTempTask.Tables(0).Rows(0).Item("task_name")

            '③	将消息库中消息内容包含企业名称和任务名称的消息确认标志改为“Y”。
            strSql = "{is_affirmed<>'Y'and project_code=" & "'" & projectID & "'" & "}"
            Dim dsTempMsg As DataSet = WfProjectMessages.GetWfProjectMessagesInfo(strSql)
            Dim tmpMsgContent, tmpAccepter As String
            Dim i As Integer
            Dim iCorp, iTask As Integer
            For i = 0 To dsTempMsg.Tables(0).Rows.Count - 1
                tmpMsgContent = IIf(IsDBNull(dsTempMsg.Tables(0).Rows(i).Item("message_content")), "", dsTempMsg.Tables(0).Rows(i).Item("message_content"))
                tmpAccepter = IIf(IsDBNull(dsTempMsg.Tables(0).Rows(i).Item("accepter")), "", dsTempMsg.Tables(0).Rows(i).Item("accepter"))
                iCorp = InStr(tmpMsgContent, tmpCorporationName)
                iTask = InStr(tmpMsgContent, tmpTaskName)
                If iCorp <> 0 And iTask <> 0 Then
                    dsTempMsg.Tables(0).Rows(i).Item("is_affirmed") = "Y"
                End If
            Next
            WfProjectMessages.UpdateWfProjectMessages(dsTempMsg)
        End If

    End Function


    '添加工作日志
    Public Function AddWorkLog(ByVal projectID As String, ByVal taskID As String, ByVal taskName As String, ByVal userID As String, ByVal taskStatus As String, ByVal startTime As DateTime, ByVal finishTime As DateTime, ByVal autoType As Integer, ByVal projectPhase As String, ByVal projectStatus As String, ByVal start_mode As String)
        Dim workLog As New WorkLog(conn, ts)
        Dim dsTempWorkLog As DataSet = workLog.GetWorkLogInfo("null")

        '获取该任务的角色ID
        Dim strSql As String = "{project_code=" & "'" & projectID & "'" & " and task_id=" & "'" & taskID & "'" & "}"
        Dim dsAttendRole As DataSet = WfProjectTaskAttendee.GetWfProjectTaskAttendeeInfo(strSql)

        '异常处理  
        If dsAttendRole.Tables(0).Rows.Count = 0 Then
            Dim wfErr As New WorkFlowErr
            wfErr.ThrowNoRecordkErr(dsAttendRole.Tables(0))
            Throw wfErr
        End If

        Dim roleID As String = Trim(dsAttendRole.Tables(0).Rows(0).Item("role_id"))

        Dim newRow As DataRow = dsTempWorkLog.Tables(0).NewRow
        With newRow
            .Item("project_code") = projectID
            .Item("task_id") = taskID
            .Item("task_name") = taskName
            .Item("role_id") = roleID
            .Item("attend_person") = userID
            .Item("task_status") = taskStatus
            .Item("start_time") = startTime
            .Item("finish_time") = finishTime
            .Item("auto") = autoType
            .Item("project_phase") = projectPhase
            .Item("project_status") = projectStatus
            .Item("start_mode") = start_mode
        End With
        dsTempWorkLog.Tables(0).Rows.Add(newRow)
        workLog.UpdateWorkLog(dsTempWorkLog)
    End Function

    '比较判断表达式是否为真
    Private Function CompareExpression(ByVal workFlowID As String, ByVal projectID As String, ByVal taskID As String, ByVal expFlag As String, ByVal transCondition As String) As Boolean
        '定义条件处理接口
        Dim iCondition As ICondition

        '一般的情况
        iCondition = New ImplCommon

        Return iCondition.GetResult(workFlowID, projectID, taskID, expFlag, transCondition)

    End Function

    'FiliterJeeDummyTask（ProjectID、ShiftTaskSet）
    '过滤后继活动集的虚活动，获取业务活动。
    Private Function FiliterJeeDummyTask(ByVal workflowID As String, ByVal projectID As String, ByVal ShiftTaskSet As DataSet, ByVal finishedFlag As String, ByVal userID As String) As DataSet

        '①	Vtask=True
        Dim vTask As Boolean = True

        Dim strSql As String
        Dim tmpTaskID, tmpTaskType, tmpApplyTool, tmpFlowTools, nextTaskID, tmpTransCondition, tmpTransStatus, tmpPhase, tmpStatus As String
        Dim dsTempNoVtaskSet, dsTempTask, dsTempTaskTransfer, dsConditionTrue, dsProject As DataSet
        Dim newRow As DataRow
        Dim args As Object() = {conn, ts}
        Dim i, j As Integer
        Dim t As System.Type
        Do While vTask = True

            vTask = False

            strSql = "{project_code=" & "'" & projectID & "'" & "}"
            dsProject = project.GetProjectInfo(strSql)

            For i = 0 To ShiftTaskSet.Tables(0).Rows.Count - 1
                tmpStatus = Trim(IIf(IsDBNull(ShiftTaskSet.Tables(0).Rows(0).Item("project_status")), "", ShiftTaskSet.Tables(0).Rows(0).Item("project_status")))


                '②	对于转移条件为真的转移活动集ShiftTaskSet中的每个任务TaskID
                '    在任务表获取当前任务（ProjectID、TaskID）；

                tmpTaskID = Trim(ShiftTaskSet.Tables(0).Rows(i).Item("next_task"))
                strSql = "{project_code=" & "'" & projectID & "'" & " and task_id=" & "'" & tmpTaskID & "'" & "}"


                '获取任务的活动类型；
                dsTempTask = WfProjectTask.GetWfProjectTaskInfo(strSql)

                '异常处理  
                If dsTempTask.Tables(0).Rows.Count = 0 Then
                    Dim wfErr As New WorkFlowErr
                    wfErr.ThrowNoRecordkErr(dsTempTask.Tables(0))
                    Throw wfErr
                End If

                tmpTaskType = IIf(IsDBNull(dsTempTask.Tables(0).Rows(0).Item("task_type")), "", dsTempTask.Tables(0).Rows(0).Item("task_type"))
                tmpPhase = Trim(IIf(IsDBNull(dsTempTask.Tables(0).Rows(0).Item("project_phase")), "", dsTempTask.Tables(0).Rows(0).Item("project_phase")))


                '如果任务类型为“AUTO”
                If tmpTaskType = "AUTO" Then


                    If tmpPhase <> "" Then
                        dsProject.Tables(0).Rows(0).Item("phase") = tmpPhase
                    End If

                    If tmpStatus <> "" Then
                        dsProject.Tables(0).Rows(0).Item("status") = tmpStatus
                    End If

                    project.UpdateProject(dsProject)

                    'Vtask= True
                    vTask = True

                    '如果应用工具非空，调用应用工具；
                    strSql = "{project_code=" & "'" & projectID & "'" & " and task_id=" & "'" & tmpTaskID & "'" & "}"
                    dsTempTask = WfProjectTask.GetWfProjectTaskInfo(strSql)

                    '异常处理  
                    If dsTempTask.Tables(0).Rows.Count = 0 Then
                        Dim wfErr As New WorkFlowErr
                        wfErr.ThrowNoRecordkErr(dsTempTask.Tables(0))
                        Throw wfErr
                    End If

                    'tmpApplyTool = Trim(IIf(IsDBNull(dsTempTask.Tables(0).Rows(0).Item("apply_tool")), "", dsTempTask.Tables(0).Rows(0).Item("apply_tool")))

                    'If tmpApplyTool <> "" Then
                    '    '调用工具
                    '    tmpApplyTool = "BusinessRules." & tmpApplyTool
                    '    t = System.Type.GetType(tmpApplyTool)
                    '    Dim iApplyTools As IApplyTools = Activator.CreateInstance(t, args)
                    '    iApplyTools.UseApplyTools()

                    'End If

                    '如果当前任务提供流程工具，调用流程工具；
                    If Trim(IIf(IsDBNull(dsTempTask.Tables(0).Rows(0).Item("flow_tool")), "", dsTempTask.Tables(0).Rows(0).Item("flow_tool"))) <> "" Then

                        tmpFlowTools = dsTempTask.Tables(0).Rows(0).Item("flow_tool")
                        tmpFlowTools = "BusinessRules." & tmpFlowTools

                        '动态创建接口对象
                        t = System.Type.GetType(tmpFlowTools)
                        Dim iFlowTools As IFlowTools = Activator.CreateInstance(t, args)
                        iFlowTools.UseFlowTools(workflowID, projectID, tmpTaskID, finishedFlag, userID)

                    End If

                    '    获取当前任务的转移任务和转移条件；
                    strSql = "{project_code=" & "'" & projectID & "'" & " and task_id=" & "'" & tmpTaskID & "'" & "}"
                    dsTempTaskTransfer = WfProjectTaskTransfer.GetWfProjectTaskTransferInfo(strSql)

                    '    获取转移条件为真的转移任务集（如果转移条件为空，返回真）；
                    dsConditionTrue = WfProjectTaskTransfer.GetWfProjectTaskTransferInfo("null")
                    For j = 0 To dsTempTaskTransfer.Tables(0).Rows.Count - 1
                        nextTaskID = dsTempTaskTransfer.Tables(0).Rows(j).Item("next_task")
                        tmpTransCondition = Trim(dsTempTaskTransfer.Tables(0).Rows(j).Item("transfer_condition"))
                        tmpTransStatus = Trim(IIf(IsDBNull(dsTempTaskTransfer.Tables(0).Rows(j).Item("project_status")), "", dsTempTaskTransfer.Tables(0).Rows(j).Item("project_status")))

                        '判断条件是否为真
                        If CompareExpression(workflowID, projectID, tmpTaskID, ".T.", tmpTransCondition) Then

                            '构造转移条件为真的转移任务集
                            newRow = dsConditionTrue.Tables(0).NewRow()
                            With newRow
                                .Item("workflow_id") = ""
                                .Item("project_code") = projectID
                                .Item("task_id") = tmpTaskID
                                .Item("next_task") = nextTaskID
                                .Item("transfer_condition") = tmpTransCondition
                                .Item("project_status") = tmpTransStatus
                            End With
                            dsConditionTrue.Tables(0).Rows.Add(newRow)
                        End If
                    Next

                    '    将当前任务（任务类型为AUTO）从转移活动集中删除；
                    ShiftTaskSet.Tables(0).Rows(i).Delete()
                    ShiftTaskSet.AcceptChanges()

                    '    将转移条件为真的转移任务集添加到ShiftTaskSet；
                    For j = 0 To dsConditionTrue.Tables(0).Rows.Count - 1
                        newRow = ShiftTaskSet.Tables(0).NewRow
                        With newRow
                            .Item("workflow_id") = ""
                            .Item("project_code") = projectID
                            .Item("task_id") = dsConditionTrue.Tables(0).Rows(j).Item("task_id")
                            .Item("next_task") = dsConditionTrue.Tables(0).Rows(j).Item("next_task")
                            .Item("transfer_condition") = dsConditionTrue.Tables(0).Rows(j).Item("transfer_condition")
                            .Item("project_status") = dsConditionTrue.Tables(0).Rows(j).Item("project_status")
                        End With
                        ShiftTaskSet.Tables(0).Rows.Add(newRow)
                    Next

                    '如果找到虚任务,返回本次操作
                    Exit For

                End If


            Next
        Loop

        Return ShiftTaskSet

    End Function


    '格式：AddTaskTrackRecord（ProjectID,Workflow_id,TaskID,StartupTask）
    Public Function AddTaskTrackRecord(ByVal workflowID As String, ByVal projectID As String, ByVal finishedTaskID As String, ByVal StartupTaskID As String)
        Dim strSql As String
        Dim i As Integer

        '①	创建Project_Track对象;
        Dim dsProjectTrack As DataSet
        dsProjectTrack = WfProjectTrack.GetWfProjectTrackInfo("null")

        '②	将参数值分别赋予新增对象的Project_Code、Workflow_id、FinishedTask、StartupTask属性,Status属性置为“P”；；
        Dim newRow As DataRow = dsProjectTrack.Tables(0).NewRow
        With newRow
            .Item("project_code") = projectID
            .Item("workflow_id") = workflowID
            .Item("FinishedTask") = finishedTaskID
            .Item("StartupTask") = StartupTaskID
            .Item("Status") = "P"
        End With
        dsProjectTrack.Tables(0).Rows.Add(newRow)
        WfProjectTrack.UpdateWfProjectTrack(dsProjectTrack)

        '③	将所有满足Project_Code= ProjectID 、StartupTask= TaskID、Status=“P”的Project_Track对象的Status属性置为“F”；
        strSql = "{project_code=" & "'" & projectID & "'" & " and StartupTask=" & "'" & finishedTaskID & "'" & " and isnull(status,'')='P'}"
        dsProjectTrack = WfProjectTrack.GetWfProjectTrackInfo(strSql)
        For i = 0 To dsProjectTrack.Tables(0).Rows.Count - 1
            dsProjectTrack.Tables(0).Rows(i).Item("Status") = "F"
        Next
        WfProjectTrack.UpdateWfProjectTrack(dsProjectTrack)

    End Function

    '启动任务
    ' 设置前置任务，前置任务ID和前置任务责任人
    Public Function StartupTask(ByVal workFlowID As String, ByVal projectID As String, ByVal taskID As String, ByVal rollbackMsg As String, ByVal responserID As String, ByVal submitTaskID As String, ByVal submitUser As String)

        '调用原启动任务方法
        StartupTask(workFlowID, projectID, taskID, rollbackMsg, responserID)

        Dim strSql As String
        strSql = "{project_code=" & "'" & projectID & "'" & " and task_id=" & "'" & taskID & "'" & "}"
        Dim dsTempTaskStatus As DataSet = WfProjectTaskAttendee.GetWfProjectTaskAttendeeInfo(strSql)
        Dim i As Integer
        For i = 0 To dsTempTaskStatus.Tables(0).Rows.Count - 1
            '2005-03-18 yjf add 设置前置任务，前置任务ID和前置任务责任人
            dsTempTaskStatus.Tables(0).Rows(i).Item("previous_task_id") = submitTaskID
            dsTempTaskStatus.Tables(0).Rows(i).Item("previous_task_attendee") = submitUser
        Next

        WfProjectTaskAttendee.UpdateWfProjectTaskAttendee(dsTempTaskStatus)
    End Function

    '启动任务
    Public Function StartupTask(ByVal workFlowID As String, ByVal projectID As String, ByVal taskID As String, ByVal rollbackMsg As String, ByVal responserID As String)

        '在任务表获取与参数(模板ID、项目ID、任务ID)匹配的任务
        Dim strSql As String = "{project_code=" & "'" & projectID & "'" & " and task_id=" & "'" & taskID & "'" & "}"
        Dim dsTempTask As DataSet = WfProjectTask.GetWfProjectTaskInfo(strSql)

        '获取系统时间；在任务表任务的开始时间改为系统时间
        Dim sysTime As DateTime = Now

        '异常处理
        If dsTempTask.Tables(0).Rows.Count = 0 Then
            Dim wfErr As New WorkFlowErr()
            wfErr.ThrowNoRecordkErr(dsTempTask.Tables(0))
            Throw wfErr
        End If

        dsTempTask.Tables(0).Rows(0).Item("start_time") = sysTime

        '如果启动任务的启动模式为"Manual",将其置空
        Dim tmpStartMode As String = Trim(IIf(IsDBNull(dsTempTask.Tables(0).Rows(0).Item("start_mode")), "", dsTempTask.Tables(0).Rows(0).Item("start_mode")))
        If tmpStartMode = "manual" Then
            dsTempTask.Tables(0).Rows(0).Item("start_mode") = DBNull.Value
        End If

        WfProjectTask.UpdateWfProjectTask(dsTempTask)


        '获取任务是否需发送消息
        Dim bTmp As Boolean = False
        If Not dsTempTask.Tables(0).Rows(0).Item("hasMessage") Is DBNull.Value Then
            bTmp = dsTempTask.Tables(0).Rows(0).Item("hasMessage")
        End If

        Dim tmpAttend, tmpBranch As String


        '如果任务参与人为空(非分配角色)(有委托权限的角色有且只有一条任务记录)


        '  调用getTaskActor（RoleID）获取任务参与人；
        '  将当前任务的参与人置为获取的任务参与人；

        Dim dsTempTaskAttend As DataSet = WfProjectTaskAttendee.GetWfProjectTaskAttendeeInfo(strSql)

        '异常处理 
        If dsTempTaskAttend.Tables(0).Rows.Count = 0 Then
            Dim wfErr As New WorkFlowErr()
            wfErr.ThrowNoRecordkErr(dsTempTaskAttend.Tables(0))
            Throw wfErr
        End If

        'qxd modify 2004-10-11
        '为了解决一个任务有两个角色做的问题。
        Dim k, count As Integer

        count = dsTempTaskAttend.Tables(0).Rows.Count

        Dim dsTemp As DataSet

        For k = 0 To count - 1
            tmpAttend = IIf(IsDBNull(dsTempTaskAttend.Tables(0).Rows(k).Item("attend_person")), "", dsTempTaskAttend.Tables(0).Rows(k).Item("attend_person"))
            Dim tmpRoleID As String = dsTempTaskAttend.Tables(0).Rows(k).Item("role_id")

            '发送回退消息
            If rollbackMsg <> "" Then
                AddMsg(projectID, taskID, rollbackMsg, tmpAttend, responserID)
            End If

            If tmpAttend = "" Then

                strSql = "{project_code='" & projectID & "'}"
                dsTemp = project.GetProjectInfo(strSql)

                '异常处理  
                If dsTemp.Tables(0).Rows.Count = 0 Then
                    Dim wfErr As New WorkFlowErr()
                    wfErr.ThrowNoRecordkErr(dsTemp.Tables(0))
                    Throw wfErr
                End If

                tmpBranch = IIf(IsDBNull(dsTemp.Tables(0).Rows(0).Item("applicantTeam_name")), "", dsTemp.Tables(0).Rows(0).Item("applicantTeam_name"))

                '调用getTaskActor（RoleID，分支机构）获取任务参与人ACTOR；
                tmpAttend = getTaskActor(projectID, taskID, tmpRoleID, tmpBranch)

                '如果ACTOR为空，调用getTaskActor（RoleID）获取任务参与人ACTOR；
                If tmpAttend = "" Then

                    '获取分支机构的上级机构
                    Dim Branch As New Branch(conn, ts)
                    strSql = "{branch_name=" & "'" & tmpBranch & "'" & "}"
                    Dim dsBranch As DataSet = Branch.GetBranch(strSql)

                    '异常处理  
                    If dsBranch.Tables(0).Rows.Count = 0 Then
                        Dim wfErr As New WorkFlowErr()
                        wfErr.ThrowNoRecordkErr(dsBranch.Tables(0))
                        Throw wfErr
                    End If

                    Dim tmpSuper As String = IIf(IsDBNull(dsBranch.Tables(0).Rows(0).Item("superior_branch")), "", dsBranch.Tables(0).Rows(0).Item("superior_branch"))

                    '获取上级机构的参与人ACTOR
                    tmpAttend = getTaskActor(projectID, taskID, tmpRoleID, tmpSuper)

                    'tmpAttend = getTaskActor(tmpRoleID)
                End If

                strSql = "{project_code=" & "'" & projectID & "'" & " and task_id=" & "'" & taskID & "'" & "}"
                dsTempTaskAttend = WfProjectTaskAttendee.GetWfProjectTaskAttendeeInfo(strSql)

                dsTempTaskAttend.Tables(0).Rows(k).Item("attend_person") = tmpAttend
                WfProjectTaskAttendee.UpdateWfProjectTaskAttendee(dsTempTaskAttend)


            End If

            '2007-10-16 yjf edit 更新消息与任务刷新标记位
            If tmpAttend <> "" Then

                strSql = "{staff_name='" & tmpAttend & "'}"
                dsTemp = staff.FetchStaff(strSql)

                '异常处理  
                If dsTemp.Tables(0).Rows.Count = 0 Then
                    Dim wfErr As New WorkFlowErr
                    wfErr.ThrowNoRecordkErr(dsTemp.Tables(0))
                    Throw wfErr
                End If

                If IIf(IsDBNull(dsTemp.Tables(0).Rows(0).Item("DoScan")), False, dsTemp.Tables(0).Rows(0).Item("DoScan")) = False Then
                    dsTemp.Tables(0).Rows(0).Item("DoScan") = 1
                    staff.UpdateStaff(dsTemp)
                End If

            End If
        Next
        'qxd modify 2004-10-11 end 


        '将每个参与人的转移任务状态置为“P”
        Dim TimingServer As New TimingServer(conn, ts, True, True)
        Dim tmpUserID As String
        strSql = "{project_code=" & "'" & projectID & "'" & " and task_id=" & "'" & taskID & "'" & "}"
        Dim dsTempTaskStatus As DataSet = WfProjectTaskAttendee.GetWfProjectTaskAttendeeInfo(strSql)
        Dim i As Integer
        For i = 0 To dsTempTaskStatus.Tables(0).Rows.Count - 1
            tmpUserID = Trim(dsTempTaskStatus.Tables(0).Rows(i).Item("attend_person"))
            dsTempTaskStatus.Tables(0).Rows(i).Item("task_status") = "P"
            '发送消息
            If bTmp Then
                TimingServer.AddMsg(workFlowID, projectID, taskID, tmpUserID, "16", "N")
            End If
        Next

        WfProjectTaskAttendee.UpdateWfProjectTaskAttendee(dsTempTaskStatus)

        '在定时活动查找与当前任务ID匹配的定时任务；
        strSql = "{project_code=" & "'" & projectID & "'" & " and task_id=" & "'" & taskID & "'" & " and type='A'}"
        Dim dsTempTimingTask As DataSet = WfProjectTimingTask.GetWfProjectTimingTaskInfo(strSql)


        Dim tmpTimeLimit As Integer

        '将所有定时任务的开始时间置为任务的开始时间+提示时间
        '将所有定时任务表的状态置为“P”；
        Dim newRow As DataRow
        '2010-8-3 yjf add 逾期消息及登记还款证明书消息除外（因为这个两个逾期消息在ImplAddRefundPlan接口添加进去以后，会启动任务，启动任务的时候会将其预警消息的启动时间推迟，置为：启动时间＝当前时间＋提示间隔）
        If taskID <> "OverdueRecord" And taskID <> "RecordRefundCertificate" Then

            For i = 0 To dsTempTimingTask.Tables(0).Rows.Count - 1
                '提示期限
                tmpTimeLimit = dsTempTimingTask.Tables(0).Rows(i).Item("time_limit")
                newRow = dsTempTimingTask.Tables(0).Rows(i)
                With newRow
                    .Item("start_time") = DateAdd(DateInterval.Hour, tmpTimeLimit * 24, sysTime) '启动时间＝当前时间＋提示间隔
                    .Item("status") = "P"
                End With
            Next

        End If

        WfProjectTimingTask.UpdateWfProjectTimingTask(dsTempTimingTask)


    End Function

    '发送消息
    Private Function AddMsg(ByVal projectID As String, ByVal taskID As String, ByVal msg As String, ByVal accepterID As String, ByVal respsonserID As String)

        '⑥	在消息库添加消息（提示消息、员工、“N”）；
        '  组合消息内容
        Dim msgContent As String
        msgContent = respsonserID & " " & msg
        Dim dsTempTaskMessages As DataSet
        dsTempTaskMessages = WfProjectMessages.GetWfProjectMessagesInfo("null")
        Dim newRow As DataRow = dsTempTaskMessages.Tables(0).NewRow
        ' 添加消息
        With newRow
            .Item("project_code") = projectID
            .Item("message_content") = msgContent
            .Item("accepter") = accepterID
            .Item("send_time") = Now
            .Item("is_affirmed") = "N"
        End With
        dsTempTaskMessages.Tables(0).Rows.Add(newRow)
        WfProjectMessages.UpdateWfProjectMessages(dsTempTaskMessages)

    End Function


    Public Function getTaskActor(ByVal projectID As String, ByVal taskID As String, ByVal roleID As String, ByVal branch As String) As String
        '①	在STAFF-ROLE表获取角色ID=RoleID的staff_name和consigner;
        Dim strSql As String
        Dim i As Integer
        Dim role As New Role(conn, ts)
        strSql = "{role_id=" & "'" & roleID & "'" & "}"
        Dim dsTempStaff As DataSet = role.GetStaffRole(strSql)

        '异常处理  
        If dsTempStaff.Tables(0).Rows.Count = 0 Then
            Dim wfErr As New WorkFlowErr
            wfErr.ThrowNoRecordkErr(dsTempStaff.Tables(0))
            Throw wfErr
        End If

        Dim tmpStaffName As String
        Dim dsTemp As DataSet
        Dim staff As New Staff(conn, ts)
        Dim isFound As Boolean
        Dim iStaff As Integer
        For i = 0 To dsTempStaff.Tables(0).Rows.Count - 1
            '②	Staff_name=staff_name；
            tmpStaffName = Trim(dsTempStaff.Tables(0).Rows(i).Item("staff_name"))
            strSql = "{staff_name=" & "'" & tmpStaffName & "'" & "}"
            dsTemp = staff.FetchStaff(strSql)

            '异常处理  
            If dsTemp.Tables(0).Rows.Count = 0 Then
                Dim wfErr As New WorkFlowErr
                wfErr.ThrowNoRecordkErr(dsTemp.Tables(0))
                Throw wfErr
            End If

            If IIf(IsDBNull(dsTemp.Tables(0).Rows(0).Item("branch_name")), "", dsTemp.Tables(0).Rows(0).Item("branch_name")) = branch Then
                isFound = True
                iStaff = i
                Exit For
            End If
        Next

        '如果在分支机构找到参与人
        If isFound Then

            Dim tmpConsigner As String = Trim(IIf(IsDBNull(dsTempStaff.Tables(0).Rows(iStaff).Item("consigner")), "", dsTempStaff.Tables(0).Rows(iStaff).Item("consigner")))

            If tmpConsigner <> "" Then
                tmpStaffName = tmpConsigner

                '将原委托人填入consinger中
                Dim tmpSrcPerson As String = Trim(IIf(IsDBNull(dsTempStaff.Tables(0).Rows(iStaff).Item("staff_name")), "", dsTempStaff.Tables(0).Rows(iStaff).Item("staff_name")))
                Dim dsConsinger As DataSet
                strSql = "{project_code='" & projectID & "' and task_id='" & taskID & "'}"
                dsConsinger = WfProjectTaskAttendee.GetWfProjectTaskAttendeeInfo(strSql)
                For i = 0 To dsTemp.Tables(0).Rows.Count - 1
                    dsConsinger.Tables(0).Rows(i).Item("consigner") = tmpSrcPerson
                Next

                WfProjectTaskAttendee.UpdateWfProjectTaskAttendee(dsConsinger)

            End If


            '⑤	返回Staff_name；
            Return tmpStaffName
        Else

            Return ""

        End If

    End Function



    '流程恢复
    Public Function resumeProcess(ByVal projectID As String)
        '①	在任务表获取项目编码指定的任务状态为“W”的任务；
        Dim i, j As Integer
        Dim sysTime As DateTime = Now
        Dim strSql As String
        Dim dsTempAttendee, dsTempTask, dstTempTimingTask As DataSet
        Dim tmpTaskID As String
        strSql = "{project_code=" & "'" & projectID & "'" & " and task_status='W'" & "}"
        dsTempAttendee = WfProjectTaskAttendee.GetWfProjectTaskAttendeeInfo(strSql)

        '②	将状态为“W”的任务状态置为“P”；
        For i = 0 To dsTempAttendee.Tables(0).Rows.Count - 1
            dsTempAttendee.Tables(0).Rows(i).Item("task_status") = "P"

            '③	获取系统时间；
            '④	暂停结束时间置为系统时间；
            tmpTaskID = Trim(dsTempAttendee.Tables(0).Rows(i).Item("task_id"))
            strSql = "{project_code=" & "'" & projectID & "'" & " and task_id=" & "'" & tmpTaskID & "'" & "}"
            dsTempTask = WfProjectTask.GetWfProjectTaskInfo(strSql)
            For j = 0 To dsTempTask.Tables(0).Rows.Count - 1
                dsTempTask.Tables(0).Rows(j).Item("pause_end_time") = sysTime
            Next
            WfProjectTask.UpdateWfProjectTask(dsTempTask)
        Next
        WfProjectTaskAttendee.UpdateWfProjectTaskAttendee(dsTempAttendee)

        '⑤	在定时任务表中将指定工作流定时任务状态为“W”的任务开始时间改为开始时间+(暂停结束时间-暂停开始时间)； 
        strSql = "{project_code=" & "'" & projectID & "'" & " and status='W'" & "}"
        dstTempTimingTask = WfProjectTimingTask.GetWfProjectTimingTaskInfo(strSql)
        Dim tmpWorkFlowID As String
        Dim tmpStartTime, tmpPauseEndTime, tmpPauseStartTime As DateTime

        '⑥	将指定工作流定时任务状态为“W”的任务状态改为“P”； 
        For i = 0 To dstTempTimingTask.Tables(0).Rows.Count - 1
            tmpWorkFlowID = dstTempTimingTask.Tables(0).Rows(i).Item("workflow_id")
            tmpTaskID = dstTempTimingTask.Tables(0).Rows(i).Item("task_id")
            strSql = "{project_code=" & "'" & projectID & "'" & " and workflow_id=" & "'" & tmpWorkFlowID & "'" & " and task_id=" & "'" & tmpTaskID & "'" & "}"
            dsTempTask = WfProjectTask.GetWfProjectTaskInfo(strSql)

            '异常处理  
            If dsTempTask.Tables(0).Rows.Count = 0 Then
                Dim wfErr As New WorkFlowErr
                wfErr.ThrowNoRecordkErr(dsTempTask.Tables(0))
                Throw wfErr
            End If

            tmpStartTime = CDate(FormatDateTime(IIf(IsDBNull(dsTempTask.Tables(0).Rows(0).Item("start_time")), Now, dsTempTask.Tables(0).Rows(0).Item("start_time")), DateFormat.ShortDate))
            tmpPauseEndTime = CDate(FormatDateTime(IIf(IsDBNull(dsTempTask.Tables(0).Rows(0).Item("pause_end_time")), Now, dsTempTask.Tables(0).Rows(0).Item("pause_end_time"))))
            tmpPauseStartTime = CDate(FormatDateTime(IIf(IsDBNull(dsTempTask.Tables(0).Rows(0).Item("pause_start_time")), Now, dsTempTask.Tables(0).Rows(0).Item("pause_start_time"))))

            dstTempTimingTask.Tables(0).Rows(i).Item("start_time") = DateAdd(DateInterval.Day, DateDiff(DateInterval.Day, tmpPauseStartTime, tmpPauseEndTime), tmpStartTime)
            dstTempTimingTask.Tables(0).Rows(i).Item("status") = "P"
        Next
        WfProjectTimingTask.UpdateWfProjectTimingTask(dstTempTimingTask)

        '将定时任务表中的恢复工作流任务的状态置为'E'
        strSql = "{project_code=" & "'" & projectID & "'" & " and task_id='WakeProject'" & "}"
        dstTempTimingTask = WfProjectTimingTask.GetWfProjectTimingTaskInfo(strSql)

        For i = 0 To dstTempTimingTask.Tables(0).Rows.Count - 1
            dstTempTimingTask.Tables(0).Rows(i).Item("status") = "E"
        Next
        WfProjectTimingTask.UpdateWfProjectTimingTask(dstTempTimingTask)


    End Function
End Class
